package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.common.TokenHolder;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealService;
import com.itheima.vo.DishWithFlavorVo;
import com.itheima.vo.SetmealAndCateAndSetmealDishVO;
import com.itheima.vo.SetmealAndCategory;
import com.itheima.vo.SetmealAndSetmealDishVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 小帅蓝
 * @Date 2022/6/20 8:46
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;

    @Override
    public boolean addSetmeal(SetmealAndSetmealDishVO setmealAndSetmealDishVO) {
        //添加套餐
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(setmealAndSetmealDishVO, setmeal);
        setmeal.setId(new Snowflake().nextIdStr());
        setmeal.setCreateUser(TokenHolder.getCurrentId());
        setmeal.setUpdateUser(TokenHolder.getCurrentId());
        int result = setmealMapper.addSetmeal(setmeal);

        //添加套餐菜品
        List<SetmealDish> setmealDishes = setmealAndSetmealDishVO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setId(new Snowflake().nextIdStr());
            setmealDish.setSetmealId(setmeal.getId());
            setmealDish.setCreateUser(TokenHolder.getCurrentId());
            setmealDish.setUpdateUser(TokenHolder.getCurrentId());
        }
        int result2 = setmealDishMapper.addSetmealDish(setmealDishes);
        return result > 0 && result2 == setmealDishes.size();
    }

    @Override
    public Integer getCount(String name) {
        return setmealMapper.getCount(name);
    }

    @Override
    public List<SetmealAndCategory> query(String name, Integer page, Integer pageSize) {
        Integer offset = getOffset(page, pageSize);
        return setmealMapper.queryAll(name, offset, pageSize);
    }

    @Override
    public boolean deleteSetmeal(String ids) {
        String[] idsArray = ids.split(",");
        List<String> idsList = Arrays.stream(idsArray).collect(Collectors.toList());
        //先查询套餐状态
        List<Integer> status = setmealMapper.seleteStatus(idsList);
        for (Integer s : status) {
            if (ObjectUtil.isNull(s)) {
                continue;
            }
            if (s.equals(1)) {
                return false;
            }
        }
        Integer result = setmealMapper.deleteSetmeal(idsList);
        return result == idsList.size();
    }

    @Override
    public SetmealAndCateAndSetmealDishVO queryById(String id) {
        SetmealAndCateAndSetmealDishVO vo = setmealMapper.queryById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectById(id);
        vo.setSetmealDishes(setmealDishes);
        return vo;
    }

    @Override
    public boolean updateSetmeal(SetmealAndCateAndSetmealDishVO scs) {
        //删除套餐里面的菜品
        setmealDishMapper.deleteById(scs.getId());
        //添加菜品
        //添加套餐菜品
        List<SetmealDish> setmealDishes = scs.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setId(new Snowflake().nextIdStr());
            setmealDish.setSetmealId(scs.getId());
            setmealDish.setCreateUser(TokenHolder.getCurrentId());
            setmealDish.setUpdateUser(TokenHolder.getCurrentId());
        }

        setmealDishMapper.addSetmealDish(setmealDishes);
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(scs, setmeal);
        setmeal.setUpdateUser(TokenHolder.getCurrentId());
        int row = setmealMapper.updateSetmeal(setmeal);
        return row > 0;
    }

    @Override
    public boolean updateStatus(String ids, Integer status) {
        String[] idsArr = ids.split(",");
        List<String> idList = Arrays.stream(idsArr).collect(Collectors.toList());
        int result = setmealMapper.updateStatus(idList, status);
        return result == idList.size();
    }

    @Override
    public List<DishWithFlavorVo> selectListById(String setmealId) {
        return setmealDishMapper.selectDishBySetmealId(setmealId);
    }

    @Override
    public List<SetmealAndCategory> selectList(String categoryId, Integer status) {
        return setmealMapper.selectList(categoryId, status);
    }

    /**
     * 获取起始页
     *
     * @param page     当前页
     * @param pageSize 每页条数
     * @return 每页起始数
     */
    public Integer getOffset(Integer page, Integer pageSize) {
        return (page - 1) * pageSize;
    }
}
